import { _decorator, CCInteger, Component, Node, UITransform, v3 } from 'cc';
import { gameConfig } from './Configs';
const { ccclass, property } = _decorator;

@ccclass('Scroll')
export class Scroll extends Component {
    @property([Node])
    items: Node[] = []
    @property(CCInteger)
    speed: number = 0;
    @property(CCInteger)
    delteX: number = 0;

    private _width0: number = 0;
    private _width1: number = 0;
    start() {
        this._width0 = this.items[0].getComponent(UITransform).width;
        this._width1 = this.items[1].getComponent(UITransform).width;
    }
    update(deltaTime: number) {
        if (!gameConfig.isScroll) {
            return;
        }
        let x0 = this.items[0].position.x;
        let x1 = this.items[1].position.x;

        x0 += this.speed * deltaTime;
        x1 += this.speed * deltaTime;
        if (x0 < -this._width0) {
            x0 = x1 + this._width1 + this.delteX;
        }
        if (x1 < -this._width1) {
            x1 = x0 + this._width0 + this.delteX;
        }
        this.items[0].setPosition(v3(x0, 0, 0));
        this.items[1].setPosition(v3(x1, 0, 0));

    }


}


